#define _CRT_SECURE_NO_WARNINGS 1
#include"HeatSort.h"

void swap(int* a, int* b)
{
	assert(a && b);
	int c = *a;
	*a = *b;
	*b = c;
}
void AdjustDown(int* a, int prent,int size)
{
	assert(a);
	int child = child = prent * 2 + 1;

	while (child < size)
	{
		if ( child + 1 < size && a[child] < a[child+1] )
		{
			child++;
		}
		if (a[prent] < a[child])
		{
			swap(a + prent, a + child);
		    prent = child;
			child = child*2+1;
		}
		else
			break;

		
	}

}
void HeapSort(int* a, int n)
{
	int prent = (n - 1) / 2;
	while (prent>=0)
	{
		AdjustDown(a,prent,n);
		prent--;
	}

	for (int i = 0; i < n - 1; i++)
	{
		swap(a , a + n - 1 - i);
		AdjustDown(a, 0,n-i-1);
	}
}